//
//  VETAssociationTableViewCell.m
//  VET
//
//  Created by BlueWind on 2018/9/3.
//  Copyright © 2018年 BlueWind. All rights reserved.
//

#import "VETAssociationTableViewCell.h"

#import "Masonry.h"

@implementation VETAssociationTableViewCell

{
    UIImageView *_associationImageView;
    
    UILabel *_associationName;
    
    UILabel *_associationSummary;
     
}

/** 返回循环利用的cell */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self= [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self loadDefaultSetting];
    }
    return self;
}

/** 加载默认UI元素和需要的数据 */
- (void)loadDefaultSetting {
    
    _associationImageView = [UIImageView new];
    [self.contentView addSubview:_associationImageView];
    [_associationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(15);
        make.size.mas_equalTo(CGSizeMake(55, 55));
    }];
    _associationImageView.layer.cornerRadius = 5;
    _associationImageView.layer.masksToBounds = YES;
    _associationImageView.contentMode = UIViewContentModeScaleAspectFill;
    _associationImageView.image = [UIImage imageNamed:@"First_PlaceHolder"];
    
    _associationName = [UILabel new];
    [self.contentView addSubview:_associationName];
    [_associationName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_associationImageView);
        make.left.equalTo(self->_associationImageView.mas_right).offset(20);
        make.right.equalTo(self.contentView).offset(-90);
    }];
    _associationName.text = @"维益链（VE）";
    
    _associationSummary = [UILabel new];
    [self.contentView addSubview:_associationSummary];
    [_associationSummary mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self->_associationImageView);
        make.left.equalTo(self->_associationImageView.mas_right).offset(20);
        make.right.equalTo(self.contentView).offset(-90);
    }];
    
    _associationSummary.text = @"在上面的菜单栏中 点击窗口 → 点击显示托管在远端仓库→弹出拉取失败后→关掉当前需要登录的安装窗口，选择离开→确定仓库拉去失败就进去了。";
    _associationSummary.textColor = [UIColor lightGrayColor];
    _associationSummary.font = [UIFont systemFontOfSize:14];
    
    _joinButton = [UIButton new];
    [self.contentView addSubview:_joinButton];
    [_joinButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_associationImageView);
        make.right.equalTo(self.contentView).offset(-15);
        make.size.mas_equalTo(CGSizeMake(60, 25));
    }];
    [_joinButton addTarget:self action:@selector(jionAssion) forControlEvents:(UIControlEventTouchUpInside)];
    _joinButton.layerCornerRadius = 6;
    _joinButton.backgroundColor = kCommonRedColor;
    _joinButton.titleLabel.font = kFont(12);
    
    _joinNum = [UILabel new];
    [self.contentView addSubview:_joinNum];
    [_joinNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_joinButton.mas_bottom).offset(8);
        make.left.equalTo(self->_joinButton);
        make.right.equalTo(self->_joinButton);
    }];
     _joinNum.textColor = [UIColor colorWithHexString:kDetailTextBlackColor];
    _joinNum.font = [UIFont systemFontOfSize:12];
    _joinNum.textAlignment = NSTextAlignmentCenter;
    _joinNum.text = @"1345加入";
    
    _jionNumCount = [UILabel new];
    [self.contentView addSubview:_jionNumCount];
    [_jionNumCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
    }];
    _jionNumCount.textColor = [UIColor colorWithHexString:kDetailTextBlackColor];
    _jionNumCount.font = [UIFont systemFontOfSize:12];
    _jionNumCount.textAlignment = NSTextAlignmentRight;
    _jionNumCount.text = @"59";
    _jionNumCount.hidden = YES;
    
}

- (void)jionAssion {
    
    if (_jionAssionAction) {
        _jionAssionAction();
    }
    
}

- (void)setIsHadJoin:(BOOL)isHadJoin {
    _isHadJoin = isHadJoin;
}

- (void)setModel:(VEAssionModel *)model {
    _model = model;
    
    _associationName.text = model.COMMUNITYNAME;
    _associationSummary.text = model.COMMUNITYPRESENT;
    
    if (self.isHadJoin == YES) {
         _jionNumCount.text = [NSString stringWithFormat:@"%ld人加入",model.JOINNUMBER];
    }else{
         _joinNum.text = [NSString stringWithFormat:@"%ld人加入",model.COMMUNITYNUMBER];
    }
    
   
    [_associationImageView setImageWithURL:[NSURL URLWithString:model.COMMUNITYPHOTO]];
    if ([model.ZHUANGTAI isEqualToString:@"已加入"]) {
        [_joinButton setTitle:@"已加入" forState:(UIControlStateNormal)];
        _joinButton.userInteractionEnabled = NO;
    } else {
        [_joinButton setTitle:@"加入" forState:(UIControlStateNormal)];
        _joinButton.userInteractionEnabled = YES;
    }
    
    
}

- (void)setIsCrearDT:(BOOL)isCrearDT {
    _isCrearDT = isCrearDT;
    if (isCrearDT == YES) {
        [_joinButton setTitle:@"选择" forState:(UIControlStateNormal)];
    } else {
        if ([self.model.ZHUANGTAI isEqualToString:@"已加入"]) {
            [_joinButton setTitle:@"已加入" forState:(UIControlStateNormal)];
            _joinButton.userInteractionEnabled = NO;
        } else {
            [_joinButton setTitle:@"加入" forState:(UIControlStateNormal)];
            _joinButton.userInteractionEnabled = YES;
        }    }
}

@end
